//引入vue-router
import VueRouter from 'vue-router'
import NotFound from '../views/404.vue'
import Register from '../views/Register'
import Login from '../views/Login'
import Home from '../views/Home'
import InfoShow from '../views/InfoShow'
import FundList from '../views/FundList'

//引入组件 

import Index from '../views/Index'
//创建一个路由器

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            redirect: '/index',
        },
        {
            path: '/index',
            name: 'index',
            component: Index,
            meta: { title: '首页' },
            children: [
                {
                    path: '',
                    component: Home,
                    meta: { title: '首页' },

                },
                {
                    path: '/home',
                    name: 'home',
                    component: Home,
                    meta: { title: '首页' },
                },
                {
                    path: '/infoshow',
                    name: 'infoshow',
                    component: InfoShow,
                    meta: { title: '个人信息' },
                },
                {
                    path: '/fundlist',
                    name: 'fundlist',
                    component: FundList,
                    meta: { title: '资金流水' },
                }
            ]
        },
        {
            path: '/register',
            name: 'register',
            component: Register,
            meta: { title: '注册' }
        },
        {
            path: '/login',
            name: 'login',
            component: Login,
            meta: { title: '登陆' }
        },
        {
            path: '*',
            name: '/404',
            component: NotFound,
            meta: { title: '没有该页面' }
        }
    ]
})

//路由守卫 
router.beforeEach((to, from, next) => {
    const isLogin = localStorage.eleToken ? true : false
    if (to.path == '/login' || to.path == '/register') {
        next()
    } else {
        isLogin ? next() : next('/login')
    }

})
router.afterEach((to) => {
    document.title = to.meta.title
})
export default router